﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using GrapeCity.ActiveReports;
using System.Reflection;
using ActiveReportsDemo.Viewers;
using System.Linq;
using System.Xml.Linq;

namespace ActiveReportsDemo
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

        }

        #region 声明全部Viewer控件，Viewer控件主要用于加载报表

        private ProductIntroduceViewer productIntroduceViewer = null;
        private PlatformViewer platformViewer = null;

        #endregion

        #region 创建实例TreeView，与报表功能无关，可以不用关心此代码

        private XDocument doc = null;

        private void BuildDemoTree()
        {
            treeView1.Nodes.Clear();

            TreeNode nodeFeature = new TreeNode();
            nodeFeature.Text = "报表功能展示";
            BuildTreeView(nodeFeature, doc.Element("TreeView").Element("Feature"), 1);
            nodeFeature.Expand();
            treeView1.Nodes.Add(nodeFeature);

            TreeNode nodeLibrary = new TreeNode();
            nodeLibrary.Text = "行业报表库";
            BuildTreeView(nodeLibrary, doc.Element("TreeView").Element("Library"), 1);
            nodeLibrary.Expand();
            treeView1.Nodes.Add(nodeLibrary);
        }

        private void BuildTreeView(TreeNode node, XElement element, int level)
        {
            var nodes = element.Elements("Node");

            foreach (var item in nodes)
            {
                if (item.HasElements)
                {
                    TreeNode subnode = new TreeNode();
                    subnode.Text = item.Attribute("Title").Value;
                    BuildTreeView(subnode, item, level + 1);
                    node.Nodes.Add(subnode);
                }
                else
                {
                    TreeNode leaf = new TreeNode();
                    leaf.Text = item.Attribute("Title").Value;
                    leaf.Tag = item.Attribute("ReportID").Value;
                    node.Nodes.Add(leaf);
                    if (level < 2)
                    {
                        node.Expand();
                    }
                }
            }
        }

        #endregion

        // 初始化示例程序设置
        private void MainForm_Load(object sender, EventArgs e)
        {
            this.SuspendLayout();
            
            // 加载示例说明文件
            doc = System.Xml.Linq.XDocument.Load(@"Data\ReportDescription.xml");

            // 创建示例TreeView
            BuildDemoTree();

            // 添加示例首页面
            productIntroduceViewer = new ProductIntroduceViewer();
            productIntroduceViewer.Dock = DockStyle.Fill;
            productIntroduceViewer.HomeItemClicked += new EventHandler(productIntroduceViewer_HomeItemClicked);
            this.panelMain.Controls.Add(productIntroduceViewer);
            this.treeView1.SelectedNode = this.treeView1.Nodes[0];

            // 创建跨平台支持页面
            platformViewer = new PlatformViewer();
            platformViewer.Dock = DockStyle.Fill;

            this.ResumeLayout(false);
        }

        // 查看TreeView中特定的节点
        private TreeNode FindNode(TreeNode tnParent, string strValue)
        {
            if (tnParent == null) return null;
            if (tnParent.Text == strValue) return tnParent;

            TreeNode tnRet = null;
            foreach (TreeNode tn in tnParent.Nodes)
            {
                tnRet = FindNode(tn, strValue);
                if (tnRet != null) break;
            }
            return tnRet;
        }

        // 用户选择查看的报表
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            // 检查用户点击的示例名称
            if (e.Node.Text.Equals("报表功能展示"))
            {
                this.panelMain.Controls.Clear();
                this.panelMain.Controls.Add(productIntroduceViewer);
                return;
            }
            else if (e.Node.Tag == null)
            {
                return;
            }

            this.SuspendLayout();

            #region 切换报表

            string reportid = "";

            if (e.Node.Tag != null && !string.IsNullOrEmpty(e.Node.Tag.ToString()))
                reportid = e.Node.Tag.ToString();

            SetMainPanel(e.Node.Text, reportid);

            #endregion

            this.ResumeLayout(false);
        }

        // 加载用户选择的报表模板
        private void SetMainPanel(string title, string reportid)
        {
            // 获取当前示例信息
            System.Xml.Linq.XElement element = (from c in doc.Element("TreeView").Element("Reports").Elements("Report")
                                                where c.Attribute("ID").Value == reportid
                                                select c).First();

            #region 切换报表查看控件

            DemoBaseUserControl mainviewer = null;

            switch (element.Attribute("Viewer").Value.ToLower())
            {
                case "defaultviewer":
                    mainviewer = new DefaultViewer();
                    break;
                case "tocviewer":
                    mainviewer = new TocViewer();                 
                    break;
                case "annotationviewer":
                    mainviewer = new AnnotationViewer();            
                    break;
                case "advanceexportviewer":
                    mainviewer = new AdvanceExportViewer();                
                    break;
                case "exportviewer":
                    mainviewer = new ExportViewer();                   
                    break;
                case "themeviewer":
                    mainviewer = new ThemeViewer();                  
                    break;
                case "platformviewer":
                    mainviewer = new PlatformViewer();
                    break;
                default:
                    break;
            }

            mainviewer.Dock = DockStyle.Fill;
            mainviewer.CurrentReportName = element.Attribute("Report").Value;
            mainviewer.CurrentReportID = element.Attribute("ID").Value;
            panelMain.Controls.Clear();
            panelMain.Controls.Add(mainviewer);            
            mainviewer.ShowReport();

            #endregion
        }

        // 跳转到葡萄城控件官网
        private void label_Click(object sender, EventArgs e)
        {
            Control label = (sender as Control);
            System.Diagnostics.Process.Start(label.Tag.ToString());
        }

        // 产品介绍页面的点击事件
        private void productIntroduceViewer_HomeItemClicked(object sender, EventArgs e)
        {
            TitleControl title = (sender as TitleControl);
            foreach (TreeNode item in treeView1.Nodes)
            {
                TreeNode node = FindNode(item, title.ReportName);
                if (node != null)
                {
                    treeView1.SelectedNode = node;
                    break;
                }
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            BuildDemoTree();
            this.treeView1.SelectedNode = this.treeView1.Nodes[0];
        }
    }
}